#include "wificonfig.h"
IPAddress apIP(192, 168, 1, 1);
DNSServer dnsServer;
ESP8266WebServer webServer(80);

/*var*/
extern String wifiname="01234567890123456789";
extern String wifipass="01234567890123456789";
extern String ip="01234567890123456789";
extern String port="0000";
extern String username="01234567890123456789";
extern String userpass="01234567890123456789";
uint8_t MAC_array_STA[6];
char loop_wifi_config;
void(* resetFunc) (void) = 0;
/*API for wifi*/
extern "C" int setWifi(void)
{
  read_info();
  WiFi.mode(WIFI_STA); 
  WiFi.begin(wifiname,wifipass);
  delay(5000);
  if(WiFi.status()==WL_CONNECTED) {return 1;}
  for(char i = 0;i<5;i++)
      {
        WiFi.begin(wifiname,wifipass);
        delay(5000);
        if(WiFi.status()==WL_CONNECTED) {return 1;}
      }
  if(WiFi.status()!=WL_CONNECTED) delay(5000);
  return 0;
}
extern "C" int WiFiInit(void)
{
  
  if( SPIFFS.exists(config_file_name)) SPIFFS.remove(config_file_name);
  web_server();
  return 0;
}

/*private function*/
void read_info(void)
{
  File f = SPIFFS.open(config_file_name,"r");

  wifiname = f.readStringUntil('\n');
  wifipass = f.readStringUntil('\n');
  ip       = f.readStringUntil('\n');
  port     = f.readStringUntil('\n');
  username = f.readStringUntil('\n');
  userpass = f.readStringUntil('\n');

  f.close();

  #if DEBUG_ON
  Serial.println("*************************\nvoid read_info(void)");
  Serial.println("save file :\n*************************");
  Serial.println(wifiname);
  Serial.println(wifipass);
  Serial.println(ip);
  Serial.println(port);
  Serial.println(username);
  Serial.println(userpass);
  #endif  
}
void save_info(void)
{
  if( SPIFFS.exists(config_file_name)) SPIFFS.remove(config_file_name);
  
  File f = SPIFFS.open(config_file_name,"a+");

  f.print( wifiname );f.print('\n');
  f.print( wifipass );f.print('\n');
  f.print( ip       );f.print('\n');
  f.print( port     );f.print('\n');
  f.print( username );f.print('\n');
  f.print( userpass );f.print('\n');

  f.close();

  #if DEBUG_ON
  Serial.println("*************************\nvoid save_info(void)");
  Serial.println("save file :\n*************************");
  read_info();
  #endif 
}

char web_server(void)
{
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
  WiFi.softAP(AP_NAME);

  // 设置DNS服务的TTL数值为300
  dnsServer.setTTL(300);
  // 启动特定域名的DNS服务器
  dnsServer.start(DNS_PORT, "www.8266.com", apIP);

  
  //设置服务器页面信息
  webServer.on("/",web_page);
  webServer.onNotFound(callBack_page);

  webServer.begin(); //启动web服务器

  loop_wifi_config = 0;

  #if DEBUG_ON
  Serial.print("char web_server(void):  ip->");
  Serial.println(WiFi.softAPIP());
  #endif
  
  while(1)
  {
    dnsServer.processNextRequest();//处理DNS请求服务
    webServer.handleClient();
    if(loop_wifi_config) break;
  }
  webServer.close();
  dnsServer.stop();  
  WiFi.mode(WIFI_STA); 
  save_info();
  #if DEBUG_ON
  Serial.print("char web_server(void) end!!! ");
  Serial.println(" ESP.restart(); ");
  #endif
  resetFunc();
}
void web_page(void)
{
  #if DEBUG_ON
  Serial.println("用户访问了主页！！！");
  #endif
  if(SPIFFS.exists(html_file_name))
  {
    File file = SPIFFS.open(html_file_name, "r"); 
    webServer.streamFile(file, "text/html");
    file.close();
  }
  else
  {
    webServer.send(404,"text/plain","webFile don't exist !!!");
  }    
}
void callBack_page(void)
{
 if(webServer.args() == 7)
 {
  webServer.send(200, "text/plane","success!!!");
  loop_wifi_config = 1;
  wifiname = webServer.arg(0);
  wifipass = webServer.arg(1);
  ip       = webServer.arg(2);
  port     = webServer.arg(3);
  username = webServer.arg(4);
  userpass = webServer.arg(5);
 }
 #if DEBUG_ON
 Serial.println("*************************\nvoid callBack_page(void)");
 Serial.println("save file :\n*************************");
 
 Serial.println(webServer.argName(0));
 Serial.println(webServer.arg(0));
 Serial.println(webServer.argName(1));
 Serial.println(webServer.arg(1));
 Serial.println(webServer.argName(2));
 Serial.println(webServer.arg(2));
 Serial.println(webServer.argName(3));
 Serial.println(webServer.arg(3));
 Serial.println(webServer.argName(4));
 Serial.println(webServer.arg(4));
 Serial.println(webServer.argName(5));
 Serial.println(webServer.arg(5));
 
 #endif  
}
